<?php
session_start();
    
    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // November 28, 2011
    // IN PARAMETERS : deptPosId
    // 
    // This AJAX Request gets all security group of a particular dept-pos.


if ($_POST) {
    include('../includes/siteConfig.php');
    
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    // GET VARIABLES VIA POST METHOD
    $deptPosId = $_POST['deptPosId'];
     
    // QUERY THAT GETS ALL SECURITY GROUP OF A  DEPTPOS
         $query = 
            "
                SELECT *
                FROM Dept_Pos_SecurityGroup, SecurityGroup
                WHERE Dept_Pos_SecurityGroup.deptPosId = '$deptPosId' AND
                      Dept_Pos_SecurityGroup.securityGroupId = SecurityGroup.securityGroupId
                      
            ";
        $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
        $numRows = mysql_num_rows($result);
        
        // THIS MEANS THAT THE DEPT-POS HAS CORRESPONDING SECURITY GROUPS
        if (mysql_num_rows($result) > 0) {
            // SAVE QUERY RESULTS TO AN ARRAY > $row. Each row will be placed to a list ($feed).  
            while ($row = mysql_fetch_array($result)) {
                $jsondata = array();
                $jsondata['securityGroupId'] = $row['securityGroupId'];
                $jsondata['securityGroupName'] = $row['securityGroupName'];
                
                $feed[] = $jsondata;
            }

        echo json_encode($feed);
        }
        
        else {
            echo json_encode(0);
        }
   
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
